package screen.component
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	
	import helper.ValueObject;
	
	import screen.CamiScreen;

	public class Popup extends UI
	{
		private var _popup_mc:MovieClip
		private const _TUTO_NO1:Number = 4;
		private const _TUTO_NO2:Number = 4;
		private const _TUTO_NO3:Number = 4;
		private const _TUTORIAL_AVATAR_FOLDER:String="../assets/tutorial";
		private const _TUTORIAL_UNIT_WIDTH:Number = 760;
		private const _current_cami_airplane_arr:Array=[];
		private var _current_group:MovieClip;
		private var _tutorial_group1:MovieClip;
		private var _tutorial_group2:MovieClip;
		private var _tutorial_group3:MovieClip;
		public static var instance:Popup;
		public function Popup()
		{
			if(!instance){
				instance = this;
			}
			init()
		}
		public static function getInsctance():Popup{
			if(!instance){
				instance = new Popup()
			}
			return instance;
		}
		private function init():void{
			_popup_mc = ValueObject.C1_RESOURCE.getExportedAsset("Popup") as MovieClip;
			popupInit(_popup_mc);
			_tutorial_group1 = new MovieClip();
			_popup_mc.contentMc.addChild(_tutorial_group1);
			_tutorial_group2 = new MovieClip();
			_tutorial_group2.visible = false;
			_popup_mc.contentMc.addChild(_tutorial_group2);
			_tutorial_group3 = new MovieClip();
			_tutorial_group3.visible = false;
			_popup_mc.contentMc.addChild(_tutorial_group3);
			with(_popup_mc.contentMc){
				_tutorial_group1.x=_tutorial_group2.x = _tutorial_group3.x = maskMc.x;
				_tutorial_group1.y =_tutorial_group2.y = _tutorial_group3.y= maskMc.y;
				_current_group = _tutorial_group1;
				_current_group.mask = maskMc;
				sliderInit(_tutorial_group1,1,1,_tutorial_group1.x,_TUTORIAL_UNIT_WIDTH);
				sliderInit(_tutorial_group2,1,1,_tutorial_group2.x,_TUTORIAL_UNIT_WIDTH);
				sliderInit(_tutorial_group3,1,1,_tutorial_group3.x,_TUTORIAL_UNIT_WIDTH);
				
				buttonImageInit(nextBtn);
				MovieClip(nextBtn).addEventListener(MouseEvent.CLICK,nextTutoHandle);
				buttonImageInit(preBtn);
				MovieClip(preBtn).addEventListener(MouseEvent.CLICK,preTutoHandle);
				buttonImageInit(closeBtn);
				MovieClip(closeBtn).addEventListener(MouseEvent.CLICK,hide)
			}
			
			for(var i:Number=1;i<=_TUTO_NO1;i++){
				var tutorial_unit1:MovieClip = ValueObject.C1_RESOURCE.getExportedAsset("tutorialUnit") as MovieClip;
				tutorial_unit1.avatar.load(new URLRequest(_TUTORIAL_AVATAR_FOLDER + "1/" + i + ".png"));
				tutorial_unit1.x = (i-1)*_TUTORIAL_UNIT_WIDTH;
				_tutorial_group1.addChild(tutorial_unit1);
			}
			for(var j:Number=1;j<=_TUTO_NO2;j++){
				var tutorial_unit2:MovieClip = ValueObject.C1_RESOURCE.getExportedAsset("tutorialUnit") as MovieClip;
				tutorial_unit2.avatar.load(new URLRequest(_TUTORIAL_AVATAR_FOLDER + "2/" + j + ".png"));
				tutorial_unit2.x = (j-1)*_TUTORIAL_UNIT_WIDTH;
				_tutorial_group2.addChild(tutorial_unit2);
			}
			for(var k:Number=1;k<=_TUTO_NO1;k++){
				var tutorial_unit3:MovieClip = ValueObject.C1_RESOURCE.getExportedAsset("tutorialUnit") as MovieClip;
				tutorial_unit3.avatar.load(new URLRequest(_TUTORIAL_AVATAR_FOLDER + "3/" + k + ".png"));
				tutorial_unit3.x = (k-1)*_TUTORIAL_UNIT_WIDTH;
				_tutorial_group3.addChild(tutorial_unit3);
			}
			updateSliderBtn(_current_group);
	
			for(var l:Number =1;l<4;l++){
				var bt:MovieClip = _popup_mc.contentMc.getChildByName("t"+l) as MovieClip;
				buttonImageInit(bt);
				bt.addEventListener(MouseEvent.CLICK,tabBtnHandle);
			}
			
		}
		private function tabBtnHandle(e:MouseEvent):void{
			var btn:MovieClip = e.currentTarget as MovieClip;
			switch(btn.name){
				case "t1":
					_current_group = _tutorial_group1;
					_tutorial_group1.visible = true;
					_tutorial_group2.visible = false;
					_tutorial_group3.visible = false;
					break;
				case "t2":
					_current_group = _tutorial_group2;
					_tutorial_group1.visible = false;
					_tutorial_group2.visible = true;
					_tutorial_group3.visible = false;
					break;
				case "t3":
					_current_group = _tutorial_group3;
					_tutorial_group1.visible = false;
					_tutorial_group2.visible = false;
					_tutorial_group3.visible = true;
					break;
			}
			_current_group.mask =_popup_mc.contentMc.maskMc;
			updateSliderBtn(_current_group);
		}
		private function nextTutoHandle(e:MouseEvent):void{
			sliderNextClick(_current_group);
		}
		private function preTutoHandle(e:MouseEvent):void{
			sliderPreClick(_current_group);
		}
		public function show(airplane:CamiAirplane):void{
			_current_cami_airplane_arr.push(airplane);
			popupShow(_popup_mc);
			CamiScreen.getInstance().removeEventListener(MouseEvent.CLICK,Tank.getInstance().clickHandle);
		}
		private function hide(e:MouseEvent):void{
			popupHide(_popup_mc);
			while(_current_cami_airplane_arr.length>0){
				_current_cami_airplane_arr[_current_cami_airplane_arr.length-1].airplaneUp();
				_current_cami_airplane_arr.pop();
			}
			setTimeout(addEventClick,1000);
		}
		private function addEventClick():void{
			CamiScreen.getInstance().addEventListener(MouseEvent.CLICK,Tank.getInstance().clickHandle);
		}
	}
}